package testtiles;

import java.awt.Color;
import java.awt.Graphics2D;

import butines.core.game.BasicGame;
import butines.core.game.Drawing;
import butines.core.game.Image;
import butines.core.game.ImageSheet;
import butines.core.game.Input;
import butines.core.math2d.Vector2D;
import butines.game.Animation;

public class TestAnimGame extends BasicGame {
	
	public static void main(String[] args) {
		TestAnimGame game = new TestAnimGame();
		game.start();
	}

	private Vector2D center = new Vector2D();
	private Vector2D cursor = new Vector2D();
	
	private float angle;
	
	private Animation player;
	
	private Animation stay;
	private Animation walk;
	private Animation attack;
	private Animation die;
	private Animation corpse;
	
	private float speed = 1;
	
	private Resources resources;
	
	@Override
	public void update(float t) {
		if (player != null) {
			player.update(t, angle);
		}
	}

	@Override
	public void render(Graphics2D g) {
		Drawing.clear(Color.black);
		if (player != null) {
			player.draw(g, 160, 160);
		}
	}

	@Override
	protected void initialise() {
		
		center.set(gameContext.getWidth()/2, gameContext.getHeight()/2);
		
		resources = new Resources("datas/game.properties");
		
		stay   = resources.getAnimation("animation.footman.stay").copy();
		walk   = resources.getAnimation("animation.footman.walk").copy();
		attack = resources.getAnimation("animation.footman.attack").copy();
		die    = resources.getAnimation("animation.footman.die").copy();
		
		corpse = resources.getAnimation("animation.corpse.stay").copy();
	}

	@Override
	protected void release() {
		resources.release();
	}

	public void play(Animation anim) {
		if (anim == null) {
			return;
		}
		if (player == anim && player.isEnded() == false) {
			return;
		}
		player = anim;
		player.start();
		player.setSpeed(speed);
	}
	
	@Override
	public void onKey(int keyCode, boolean pressed, float tpf) {
		if (keyCode == Input.KEY_ESC) {
			gameContext.shutdown();
		}
		if (keyCode == Input.KEY_1 && pressed) {
			play(stay);
			
		}
		if (keyCode == Input.KEY_2 && pressed) {
			play(walk);
		}
		if (keyCode == Input.KEY_3 && pressed) {
			play(attack);
		}
		if (keyCode == Input.KEY_4 && pressed) {
			play(die);
		}
		if (keyCode == Input.KEY_5 && pressed) {
			play(corpse);
		}
	}
	
	@Override
	public void onButton(int x, int y, int button, boolean pressed, float tpf) {
		if (button == Input.BUTTON_1 && pressed) {
			if (player != null) {
				if (player.isEnded()) {
					player.start();
				} else {
					player.resume();
				}
			}
		}
		if (button == Input.BUTTON_2 && pressed) {
			player.setSpeed(speed = 1);
		}
		if (button == Input.BUTTON_3 && pressed) {
			if (player != null) {
				player.stop();
			}
		}
	}

	@Override
	public void onMove(int x, int y, int dx, int dy, float tpf) {
		cursor.set(x, y);
		cursor.sub(center);
		angle = cursor.theta();
	}
	
	@Override
	public void onWheel(int x, int y, int wheel, float tpf) {
		if (wheel > 0) {
			speed -= .1;
		} else if (wheel < 0) {
			speed += .1;
		}
		if (speed < .1) {
			speed = .1f;
		}
		if (speed > 3) {
			speed = 3f;
		}
		if (player != null) {
			player.setSpeed(speed);
		}
	}

}
